using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using EchoFramework;
using UnityEngine;

/// <summary>
/// 角色控制器
/// </summary>
public class PlayerController : ObjectBase
{
    private PrefabBinder prefabBinder;
    private EchoText scoreTxt;
    /// <summary>
    /// 每秒移动距离
    /// </summary>
    [SerializeField]
    private float distancePerSconed = 4f;
    
    /// <summary>
    /// 是否正在移动
    /// </summary>
    public bool isMoving;

    /// <summary>
    /// 本次移动的目标点
    /// </summary>
    private MapPosCfg targetMapPosCfg;

    /// <summary>
    /// 角色逻辑模块
    /// </summary>
    private PlayerLogicModule module;
    
    private void Awake()
    {
        prefabBinder = GetComponent<PrefabBinder>();
        scoreTxt = prefabBinder.Find("ScoreTxt").GetComponent<EchoText>();
        
        module = PlayerLogicModule.Instnace;
    }

    private void Update()
    {
        if (isMoving) return;
        if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
        {
            Move(DirectionEnum.Left);
        }
        else if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
        {
            Move(DirectionEnum.Right);

        }
        else if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
        {
            Move(DirectionEnum.Up);
            
        }else if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
        {
            Move(DirectionEnum.Down);
        }
    }

    /// <summary>
    /// 初始化
    /// </summary>
    public void Init()
    {
        isMoving = false;
    }
    
    /// <summary>
    /// 移动
    /// </summary>
    /// <param name="dir">移动方向</param>
    private void Move(DirectionEnum dir)
    {
        targetMapPosCfg = module.PlayerMapRoadItem.GetTargetMapPosCfg(dir);
        if (targetMapPosCfg == null)
            return;
        
        MapPosCfg selfMapPosCfg = module.PlayerMapRoadItem.mapPosCfg;
        if (selfMapPosCfg.PosTypeEnum == PosTypeEnum.PosPoint && !string.IsNullOrEmpty(selfMapPosCfg.Condition))
        {
            List<MapPosCfg> targetList = selfMapPosCfg.GetTargetIDCfgList();
            int index = 0;
            for (int i = 0; i < targetList.Count; i++)
            {
                if (targetList[i].ID == targetMapPosCfg.ID)
                {
                    index = i;
                    break;
                }
            }

            string[] conditionArr = selfMapPosCfg.Condition.Split('|');
            string[] cond = conditionArr[index].Split(',');
            int num = Convert.ToInt32(cond[1]);
            switch (cond[0])
            {
                case "less":
                    if (module.Score >= num)
                        return;
                    break;
                case "bigger":
                    if (module.Score <= num)
                        return;
                    break;
                case "equal":
                    if (module.Score != num)
                        return;
                    break;
            }
        }

        isMoving = true;
        transform.DOLocalMove(new Vector3(targetMapPosCfg.PosCoordinate.X, targetMapPosCfg.PosCoordinate.Y), distancePerSconed).SetEase(Ease.Linear).SetSpeedBased()
            .OnComplete(MoveComplete);
    }

    /// <summary>
    /// 移动完成
    /// </summary>
    private void MoveComplete()
    {
        module.PlayerCoor = targetMapPosCfg.PosCoordinate;
        isMoving = false;
        switch (targetMapPosCfg.PosTypeEnum)
        {
            case PosTypeEnum.MidPoint:
                List<MapPosCfg> tempCfgList = targetMapPosCfg.GetTargetIDCfgList();
                if (tempCfgList.Count > 0)
                {
                    DirectionEnum tempDir = targetMapPosCfg.GetTargetDir(tempCfgList[0].PosCoordinate);
                    Move(tempDir);
                }
                break;
            case PosTypeEnum.PosPoint:
                if (!string.IsNullOrEmpty(targetMapPosCfg.Method))
                {
                    string[] methodArr = targetMapPosCfg.Method.Split(',');
                    int num = Convert.ToInt32(methodArr[1]);
                    switch (methodArr[0])
                    {
                        case "add":
                            module.Score += num;
                            break;
                        case "reduce":
                            module.Score -= num;
                            break;
                        case "multi":
                            module.Score *= num;
                            break;
                        case "div":
                            module.Score /= num;
                            break;
                    }
                }
                break;
            case PosTypeEnum.End:
                GameLogger.LogGreen("恭喜你已达到终点！！！");
                UIViewManager.Instance.OpenWindow(UIViewID.VictoryView);
                SaveLogicModule.Instance.SaveData(module.Level, LevelStatusEnum.Pass);
                break;
        }
    }

    /// <summary>
    /// 更新分数
    /// </summary>
    public void UpdateScoreTxt()
    {
        scoreTxt.text = module.Score.ToString();
    }
}
